package com.tjrac.generalpermissions.dao;

import com.tjrac.generalpermissions.domain.Role;
import com.tjrac.generalpermissions.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author ZeNing
 * @create 2020-11-27 21:49
 */
@Repository
public interface UserDao {

    @Select("SELECT * FROM `user`")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(
                    property = "role",
                    column = "rid",
                    javaType = Role.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.RoleDao.queryById")
            )
    })
    List<User> queryAll();

    @Select("SELECT * FROM `user` WHERE username = #{username}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(
                    property = "role",
                    column = "rid",
                    javaType = Role.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.RoleDao.queryById")
            )
    })
    User queryByUsername(String username);

    @Select("select * from user where `id` = #{id}")
    @Results({
            @Result(column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(
                    property = "role",
                    column = "rid",
                    javaType = Role.class,
                    one = @One(select = "com.tjrac.generalpermissions.dao.RoleDao.queryById")
            )
    })
    User queryById(int id);

    @Insert("insert into user(`username`, `password`, `rid`) values(#{username}, #{password}, #{role.id})")
    int addUser(User user);

    @Delete("delete from user where `id` = #{id}")
    int delUser(int id);

    @Update("update user set `username` = #{username}, `password` = #{password}, rid = #{role.id} where `id` = #{id}")
    int modifyById(User user);

}
